package com.farm.service.impl;
import com.farm.dao.CropsDiseaseMapper;
import com.farm.dao.CropsMapper;
import com.farm.dao.DiseaseMapper;
import com.farm.opjo.Crops;
import com.farm.opjo.Disease;
import com.farm.opjo.CropsDisease;
import com.farm.reception.WebDisease;
import com.farm.service.DiseaseService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
import java.util.Map;

@Service
public class DiseaseServiceImpl implements DiseaseService {
    @Autowired
    private DiseaseMapper disMapper;
    @Autowired
    private CropsDiseaseMapper cropsDiseaseMapper;
    @Autowired
    private CropsMapper cropsMapper;
    /**
     * 新增
     * @param disease
     */
    @Override
    public void add(Disease disease) {
        if (disMapper.selectByPrimaryKey ( disease.getName () )==null){
            disMapper.insert ( disease );
        }
    }

    /**
     * 根据id删除
     * @param id
     */
    @Override
    public boolean delete(int id) {
        if (cropsDiseaseMapper.findByDiseaseId ( id )==null){
          disMapper.deleteByPrimaryKey ( id );
          return true;
        }
        return  false;
    }

    /**
     * 根据ID更新
     * @param webDisease
     */
    @Override
    public void update(WebDisease webDisease) {
        Disease disease = webDisease.getDisease ();
        disMapper.insert ( disease );
    }

    @Override
    public Page<WebDisease> findPage(Map searchMap, int page, int size) {

        return null;
    }
    /**
     *条件+分页查询
     * @param page
     * @param size
     * @return
     * 考虑：
     * 1.前台给什么样的参数？
     *  1.农作物的name
     *  2.病虫害的name
     * 2.前台展示的内容是什么？我们去哪里查？怎么查？
     *  1.首先没有查询条件的时候展示内容是 病虫害，农作物病虫害中间表，农作物
     *  2.有查询条件的时候 我们怎么展示 我们去哪里查？怎么查？
     *  3.查完数据怎么封装
     */


    /**
     * 构建查询对象Example的方法
     * @param searchMap
     * @return
     */
    private Example createExample(Map searchMap) {
        Example example = new Example ( Disease.class );
        Example.Criteria criteria = example.createCriteria ();
        if (searchMap!=null){
            if (searchMap.get ( "cropsName" )!=null&&"".equals ( searchMap.get ("cropsName"))){
                criteria.andEqualTo ("cropsName",searchMap.get ("cropsName"));
            }
        }if (searchMap.get ("name")!=null&&"".equals ( searchMap.get ("name"))){
            criteria.andEqualTo ("name",searchMap.get ("name"));
        }if (searchMap.get ("aspect")!=null&&"".equals ( searchMap.get ("aspect"))){
            criteria.andEqualTo ("aspect","%"+searchMap.get ("aspect")+"%");
        }
        return example;
    }
}
